package com.sensePlusVR.main;

import org.xutils.x;
import org.xutils.view.annotation.ContentView;

import android.os.Bundle;

import com.sensePlusVR.activity.guide.GuideActivity;
import com.sensePlusVR.activity.user.LoginActivity;
import com.sensePlusVR.base.BaseActivity;
import com.sensePlusVR.utils.AppManager;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.MyPageJump;
import com.sensePlusVR.utils.MySharedPreferences;
import com.sensePlusVR.utils.UserUtil;

/**
 * 
 * @author: Loren
 * @create: 2016-7-8下午2:21:39
 * @desc: LancherActivity 根据不同情况进入不同界面：第一次装则进入引导界面
 * 
 */
@ContentView(R.layout.activity_loading)
public class LoadingActivity extends BaseActivity {
	private static final String TAG = LoadingActivity.class.getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyLog.d(TAG, "onCreate...");
	}

	@Override
	public void setMyContentView() {
		x.view().inject(this);
	}

	@Override
	public void initData() {
		super.initData();
		AppManager.getInstance().addActivity(this);
		//Android应用从sdcard安装后第一次按Home键后Launcher重启问题
		   if (!isTaskRoot()) {
			   AppManager.getInstance().finishActivity(this);
	            return;
	        }
		checkedIsFirstInstant();
	}

	/**
	 * 检测是否为第一次安装
	 */
	private void checkedIsFirstInstant() {
		boolean isFirst = MySharedPreferences.getBoolean2(this,
				MySharedPreferences.NAME_NOT_CLEAR,Constant.IS_FIRST_INSTANT);
		boolean isGuestEdit = MySharedPreferences.getBoolean2(this, MySharedPreferences.NAME_NOT_CLEAR,Constant.IS_FIRST_GUESTURE_EDIT);
		if (!isFirst) {
			MyPageJump.pageJump(this, GuideActivity.class);
			AppManager.getInstance().finishActivity(this);
		}else if(!isGuestEdit){//第一次安装设置手势密码
			checked();
		} else {
			checked();
		}
	}

	public void checked() {
		//判断用户是否登陆过（token不存在），没有登陆，则进入注册，第三方登陆按钮页面；登陆过到3.
		String nowToken = MySharedPreferences.getString2(this,
				MySharedPreferences.NAME_NOT_CLEAR,Constant.LOGIN_SUCCESS_TOKEN);
		MyLog.d(TAG, "Token : " + nowToken);
		// 判断用户是否登陆过，没登录过则进入登录界面，否则直接进入主界面
		if (nowToken == null || nowToken =="") {
			MyPageJump.pageJump(LoadingActivity.this,
					LoginActivity.class);
			AppManager.getInstance().finishAllActivity();
		} else {
			Integer userId = UserUtil.getUserId();
			MyLog.d(TAG, "userId : " + userId);
			if(userId > 0){
				MyPageJump.pageJump(LoadingActivity.this, MainActivity.class);
				AppManager.getInstance().finishActivity();
			}else{
				MyPageJump.pageJump(LoadingActivity.this,
						LoginActivity.class);
				AppManager.getInstance().finishAllActivity();
			}
		}
	}
	/**
	 * 携带Token登陆
	 */
/*	private void checkServer() {
		int id = MySharedPreferences.getInteger(LoadingActivity.this,
				Constant.LOGIN_SUCCESS_ID);
		String userName = MySharedPreferences.getString(LoadingActivity.this,
				Constant.USERNAME);
		String password = MySharedPreferences.getString(LoadingActivity.this,
				Constant.PASSWORD);
		NetRequestManager.startLoginRequestResult(LoadingActivity.this,
				userName, password);
		
	}*/
}
